	<?php
	ob_end_clean();
	ob_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta http-equiv="Content-Script-Type" content="text/javascript" />
		<meta http-equiv="Content-Style-Type" content="text/css" />

	</head>
<body>
<?php
	require_once( '../system/smarty.inc' );
	require_once( '../system/mdao/Class_M_COST_FLIER.php' );
	require_once( '../system/mdao/Class_M_OTHER.php' );
	require_once( '../system/Class_FILE_CSV.php' );
	require_once( '../system/Class_ERROR.php' );
	require_once( '../system/login.inc.php' );
	require_once( '../system/aoki_m.php' );
	require_once( '../system/Class_PWD.php' );

	Class_PWD::valid_auth(AOKI_AUTH_COST_QR,$login['staff_auth']);

	$docsv_import = FALSE;

	$csvf = Class_FILE_CSV::get_upload_file();

	if ($csvf) {
		// ロケール情報の変更
		$locale = setlocale(LC_ALL, 0);
		setlocale(LC_ALL,'ja_JP.UTF-8');
//echo 'aa';
		$rowall = Class_FILE_CSV::fget_rows($csvf);

		if  (!empty($rowall)) {
		$col_heads = array(
			'matrix_cd'  , //マトリックスID
			''  , //年度
			''  , //週
			''  , //折込日
			''  , //開始日
			''  , //終了日
			'shop_cd'  , //店舗コード
			''  , //店名
			''  , //エリアAJA名
			''  , //ゾーンAJA名
			''  , //チラシ経費 本年(見込)
			'plane_cost'  , //チラシ経費 計画
			''  , //チラシ経費 前年
			''  , //チラシ経費 計画費
			''  , //チラシ経費 計画差異
			''  , //チラシ経費 前年比
			''   //チラシ経費 前年差異
		);
		

		
		
	//	$csvheads = array('ins_shop_cd','ins_shop_name','ins_co_cd','local_cd','media_name','delive_name','insert_conums','stop_date','memo');
			$csvheads    = array();
			$col_index   = array();

			foreach ($col_heads as $k => $v) {
				if (!empty($v)) {
					$csvheads[$k]  = $v;
					$col_index[$v] = $k;
				}
			}

			$key1 = $col_index['matrix_cd'];//キーです、必ず仕様します
			$key2 = $col_index['shop_cd'];

			$err = new Class_ERROR("","");
			$dao = new Class_M_COST_FLIER;

			$ngline      = 0;
			$error_times = 0;
			$error_stop  = IMPORT_STOP_MAX_ERROR_TIMES;
			$errtext     = array();
			$isng        = FALSE;
			$chk         = $dao->get_checks();

			$week_arrs  = Class_M_OTHER::get_weeks();
			$matrixs    = array();
			$shops     =  Class_M_OTHER::get_shop_array(NULL);
			foreach ($week_arrs as $kk =>$vv ) {
				//$filterdate = Class_PWD::int8_to_date_nozero($vv["flier_date"]);
				$mm = $vv["matrix_cd"];
				$matrixs[$mm] = 1 ;
			}
			foreach ($rowall as $line => $row) {
				$isng = FALSE;
				//CSV内部重複不可チェック
				foreach ($rowall as $kk=>$vv) {
					if  ($line != $kk) {
						if ($row[$key1] == $vv[$key1] && $row[$key2] == $vv[$key2] ){
							$col1 = $key1 + 1;
							$col2 = $key2 + 1;
							$isng = TRUE;
							$ngMsg_tmp[$line][] = "{$line}行と{$kk}行の[{$col1}+{$col2}]列目の値重複不可。</br>";
							$error_times++;
							if ($error_times > $error_stop) {
								break;
							}
						}
					}
				}

				//一般チェック
				foreach ( $csvheads as $k=>$v ) {
					$msg =  $err->check( $rowall[$line][$k], $chk[$v ] );
					if (!empty($msg)) {
						$isng = TRUE;
						$col  = $k+1;
						$ngMsg_tmp[$line][] = "{$line}行{$col}列目：{$msg}。";
						$error_times++;
						if ($error_times > $error_stop) {
							break;
						}
					}
				}

				if (empty($shops[$row[$key2]])) {
					$col2 = $key2 + 1;
					$isng = TRUE;
					$error_times++;
					$ngMsg_tmp[$line][]   = "{$line}行{$col2}列目：{$row[$key2]}店舗スマスタに存在しません。</br>";
				}

				//マトリックス
				if ( !empty($row[$key1])){
					if (empty($matrixs[$row[$key1]])) {
						$col1 = $key1 + 1;
						$isng = TRUE;
						$error_times++;
						$ngMsg_tmp[$line][]   = "{$line}行{$col1}列目：{$row[$key1]}マトリックスマスタに存在しません。</br>";
					}else {
						//$rowall[$line][$key1] =  $matrixs[$row[$key1]];
					}
				}

				if ( $isng ) {
					++$ngline;
					$errtext[$ngline] = $row;
					if ($error_times > $error_stop) {
						break;
					}
				}
			}

			if ($ngline > 0) {
				$errHtml = Class_FILE_CSV::format_msg($ngMsg_tmp);

				if ($error_times > $error_stop) {
					Class_FILE_CSV::write_msg_error( $errHtml ,$error_stop );

				} else {
					Class_FILE_CSV::write_msg_error( $errHtml );
				}
			} else {
				
				//DBへ更新
				try {
					$daodb = $dao->get_db();
					$dao->begin_trans();
					$i = 0;
					$percent = "";
					$percentold = "";
						foreach ( $rowall as $k=>$v ) {
							$i++;
							$percent = intval($i/$line * 100)."%";
							if  ( $percent != $percentold ) {
								Class_FILE_CSV::write_msg_doing( $percent ) ;
							}
							$percentold = $percent;
							$values  = array();

							foreach ( $csvheads as $kk => $vv ) {
								$values[$vv] = $v[$kk];
							}

							$key1values =  $values[$col_heads[$key1]];
							$key2values =  $values[$col_heads[$key2]];
							$where = sprintf(" {$col_heads[$key1]} = '%s' and {$col_heads[$key2]} = '%s' ", $dao->quote($key1values), $key2values );
							if ( $dao->exits( $where ) ) {
								if (!($dao->editbywhere($where, $values ))) {
									throw  new Exception("mcost_flier_import DB更新");
								}

							} else {
								if ( !( $dao->add($values ) ) ) {
									throw  new Exception("mcost_flier_import DB登録");
								}
							}
						}
					Class_FILE_CSV::write_msg_succed($line, "mcost_flier");
					$dao->commit_trans();

				} catch (Exception $e) {
					$dao->rollback_trans();
					Class_FILE_CSV::write_msg_error( "DB Exceptionがありました。</br>" );
				}

			}
		}else {

		$errHtml = "CSVファイルエラー1";
		Class_FILE_CSV::write_msg_error( $errHtml );

		}

		// ロケール情報の復元
		setlocale(LC_ALL, $locale);

	}else {

		$errHtml = "CSVファイルエラー2";
		Class_FILE_CSV::write_msg_error( $errHtml );
	}
?>
</body>
	</himl>
